package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee)
    {
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        if(emp==null)
        {
           return R.error("登录失败");
        }

        if(!emp.getPassword().equals(password))
        {
           return R.error("登录失败");
        }

        if(emp.getStatus()==0)
        {
          return   R.error("用户已被禁用");
        }

        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }



    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

//添加
    //
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee)
    {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

       /* Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/

        employeeService.save(employee);
        return R.success("添加员工信息成功");
    }


    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name)
    {
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Employee>lqw=new LambdaQueryWrapper<>();

        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    //修改
    @PutMapping
    public R<String>update(HttpServletRequest request,@RequestBody Employee employee)
    {
        Long employeeId = (Long) request.getSession().getAttribute("employee");
        /*employee.setUpdateUser(employeeId);
        employee.setUpdateTime(LocalDateTime.now());*/

        employeeService.updateById(employee);
        return R.success("修改成功");
    }





   //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id)
    {
        Employee byId = employeeService.getById(id);

        if(byId!=null)
        {
            return R.success(byId);
        }
        return R.error("查无此人");
    }







}
